package gui.menu;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.text.TextFormat;
import events.ResizeEvent;
import gui.menu.MainMenuItem;
import gui.buttons.SimpleButton;

/**
 * ...
 * @author Damien Mabin
 */

class MenuBar extends Sprite
{

	// Taille du bord en pixel pour le haut et le bas
	private static inline var s_BorderSize : Float = 2;
	// Marge autour des boutons du menu
	private static inline var s_ButtonVerticalSpacing : Float = 0;
	// Espace qui sépare 2 boutons du menu
	private static inline var s_ButtonHorizontalSpacing : Float = 2;
	
	private var m_MenuTrees : Array<MainMenuItem>;
	private var m_MenuButtons : Array<SimpleButton>;
	
	private var m_Style: { backgroundColor : UInt, backgroundAlpha : Float, borderColor : UInt, borderAlpha : Float, foregroundColor : UInt, foregroundAlpha : Float };
	
	public function new(?_Style:{backgroundColor : UInt, backgroundAlpha : Float, borderColor : UInt, borderAlpha : Float, foregroundColor : UInt, foregroundAlpha : Float}) 
	{
		super();
		m_MenuTrees = new Array<MainMenuItem>();
		m_MenuButtons = new Array<SimpleButton>();
		
		if (_Style == null)
		{
			m_Style = { backgroundColor : 0xBFCC85, backgroundAlpha : 1.0, borderColor : 0x748F74, borderAlpha : 1.0, foregroundColor : 0xD0DFDC, foregroundAlpha : 0.5 };
		}
		else
		{
			m_Style = _Style;
		}
		
		this.addEventListener(ResizeEvent.EVENT, OnResize);
	}
	
	public function AddMenu(f_MenuTitle:String, f_MainMenuItem:MainMenuItem = null):Void
	{
		m_MenuTrees.push(f_MainMenuItem);
		
		var menuButton : SimpleButton = new SimpleButton(f_MenuTitle, new TextFormat(), {backgroundColor : m_Style.backgroundColor, backgroundAlpha : m_Style.backgroundAlpha, borderColor : m_Style.borderColor, borderAlpha : m_Style.borderAlpha, borderSize : 0, foregroundColor : 0x748F74});
		m_MenuButtons.push(menuButton);
		this.addChild(menuButton);
		menuButton.addEventListener(MouseEvent.MOUSE_DOWN, OnMouseDown);
		
		if (f_MainMenuItem != null)
		{
			this.addChild(m_MenuTrees[m_MenuTrees.length - 1]);
			m_MenuTrees[m_MenuTrees.length - 1].Collapse();
		}
	}
	
	public function CollapseAll():Void
	{
		for (i in 0 ... m_MenuTrees.length)
		{
			m_MenuTrees[i].Collapse();
		}
	}
	
	public function OnResize(f_Evt : ResizeEvent) : Void
	{
		this.graphics.clear();
		this.graphics.beginFill(0x748F74);
		this.graphics.drawRect(0, 0, f_Evt.m_Width, f_Evt.m_Height);
		this.graphics.endFill();
		
		var menuMaxHeight : Float = -1;
		for (i in 0 ... m_MenuTrees.length)
		{
			m_MenuTrees[i].x = i*100 + (i+1) * s_ButtonHorizontalSpacing;
			m_MenuTrees[i].y = f_Evt.m_Height;
			menuMaxHeight = (menuMaxHeight > m_MenuTrees[i].height) ? menuMaxHeight : m_MenuTrees[i].height;
		}
		
		width = f_Evt.m_Width;
		height = f_Evt.m_Height + menuMaxHeight;
		
		for (i in 0 ... m_MenuButtons.length)
		{
			m_MenuButtons[i].dispatchEvent(new ResizeEvent(100, f_Evt.m_Height - 2*s_BorderSize - 2*s_ButtonVerticalSpacing));
			m_MenuButtons[i].x = i*100 + (i+1) * s_ButtonHorizontalSpacing;
			m_MenuButtons[i].y = s_BorderSize + s_ButtonVerticalSpacing;
		}
		
    }
	
	public function OnMouseDown(f_Evt : MouseEvent) : Void
	{
		var menuIndex : Int = -1;
		for(i in 0 ... m_MenuButtons.length)
		{
			if (m_MenuButtons[i] == f_Evt.currentTarget)
			{
				menuIndex = i;
			}
			else
			{
				if (m_MenuTrees[i] != null)
					if (!m_MenuTrees[i].m_Collapse)
						m_MenuTrees[i].Collapse();
			}
		}
		
		if (menuIndex >= 0)
		{
			/*
			if (m_MenuTrees[menuIndex] == null)
			{
				this.dispatchEvent(new ClickLeafEvent(m_MenuButtons[menuIndex].m_Label.text));
			}*/
			//else
			//{
				if (m_MenuTrees[menuIndex].m_Collapse)
					m_MenuTrees[menuIndex].Expand();
				else
					m_MenuTrees[menuIndex].Collapse();
			//}
		}
	}
}